package com.yunyike.controller.quotation;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yunyike.constants.CommonConstant;
import com.yunyike.constants.QuotationConstant;
import com.yunyike.constants.RequestConstant;
import com.yunyike.constants.ResponseConstant;
import com.yunyike.controller.BaseController;
import com.yunyike.dto.QuotationDTO;
import com.yunyike.exception.BusinessException;
import com.yunyike.model.Brand;
import com.yunyike.model.Quotation;
import com.yunyike.model.Supplier;
import com.yunyike.service.BrandService;
import com.yunyike.service.QuotationService;
import com.yunyike.service.SupplierService;
import com.yunyike.utils.mybatis.PageBean;

@Controller
@RequestMapping(value=RequestConstant.QUOTATION)
public class QuotationController extends BaseController {

	private static Logger log = LoggerFactory.getLogger(QuotationController.class);

	@Value("${web.fileUploadDirectory}")
	private String fileUploadDirectory;
	
	@Autowired
	private QuotationService quotationService;
	
	@Autowired
	private BrandService brandService;
	
	@Autowired
	private SupplierService supplierService;
	
	/**
	 * 跳转添加供应单页面
	 * @return
	 */
	@RequestMapping(value=RequestConstant.QUOTATION_ADD,method=RequestMethod.GET)
	public String toAddQuotation(Model model){
		
		log.info("to add quotation page.");
		
		// 获取所有品牌
		List<Brand> brands = brandService.findAllBrand();
		model.addAttribute("brands", brands);
		
		// 获取所有供应商
		List<Supplier> suppliers = supplierService.findAllSuppliers();
		model.addAttribute("suppliers", suppliers);
		
		return ResponseConstant.QUOTATION_ADD_PAGE;
	}
	
	/**
	 * 添加供应单
	 * @return
	 */
	@RequestMapping(value=RequestConstant.QUOTATION_ADD, method=RequestMethod.POST)
	public String doAddQuotation(@RequestParam("quotationFile")MultipartFile[] attachs,Quotation quotation,String [] brand){

		checkParams4AddQuotation();
		
		int insert = quotationService.addQuotation(attachs,fileUploadDirectory,quotation,brand);
		
		if(insert == CommonConstant.NUMBER_1){
			return ResponseConstant.REDIRECT+RequestConstant.QUOTATION+RequestConstant.QUOTATION_LIST;
		}
		
		throw new BusinessException();
	}
	
	/**
	 * 供应单列表页
	 * @return
	 */
	@RequestMapping(value=RequestConstant.QUOTATION_LIST)
	public String toListQuotation(Model model,Integer page, Integer status){
		
		if(page == null){
			page = CommonConstant.NUMBER_1;
		}
		if(status == null){
			status = CommonConstant.NUMBER_0;
		}
		
		// 封装pageBean
		Quotation quotation = new Quotation();
		quotation.setQuotationStatus(status);
		
		PageBean<QuotationDTO> pageBean = new PageBean<QuotationDTO>();
		pageBean.setParameter(quotation);
		pageBean.setPage(page);
		pageBean.setPageSize(QuotationConstant.QUOTATION_PAGE_SIZE);
		
		// call service
		pageBean = quotationService.findQuotationByCondition(pageBean);
		
		model.addAttribute("pageBean", pageBean);
		model.addAttribute("status", status);
		
		return ResponseConstant.QUOTATION_LIST_PAGE;
	}
	
	/**
	 * 删除供应单
	 * @param model
	 * @param quotationId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value=RequestConstant.QUOTATION_DEL+"/{quotationId}",method=RequestMethod.POST)
	public String deleteQuotation(Model model,@PathVariable String quotationId){
		
		if(StringUtils.isBlank(quotationId)){
			log.info("请求参数不正确.");
			return ResponseConstant.FAILED;
		}
		
		Quotation quotation = quotationService.selectByPrimaryKey(quotationId);
		if(quotation == null){
			log.info("quotationId：{} 未找到对应供应单信息",quotationId);
			return ResponseConstant.FAILED;
		}
		
		if(!quotation.getQuotationStatus().equals(CommonConstant.NUMBER_1)){
			log.info("quotationId：{} 的状态不为 未处理，当前状态为：{}",quotationId,quotation.getQuotationStatus());
			return ResponseConstant.FAILED;
		}
		
		int deleteResult = quotationService.deleteByPrimaryKey(quotationId);
		log.info("quotationService.deleteByPrimaryKey result:{}",deleteResult);
		
		if(deleteResult != 1){
			return ResponseConstant.FAILED;
		}
		
		return ResponseConstant.SUCCESS;
	}
	
	private void checkParams4AddQuotation(){
		
	}
}
